//
//  HsLCAudioFloatView.m
//  TestIos
//
//  Created by hs on 2020/10/28.
//  Copyright © 2020 Liyadong. All rights reserved.
//

#import "HsLCAudioFloatView.h"

@interface HsLCAudioFloatView ()

@property (nonatomic, assign) CGPoint startLocation;

 
@end


@implementation HsLCAudioFloatView


- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        self.userInteractionEnabled = YES;
        
        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapMe)];
        [self addGestureRecognizer:tap];

        
    }
    return self;
}
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
    CGPoint pt = [[touches anyObject] locationInView:self];
    self.startLocation = pt;
    [[self superview] bringSubviewToFront:self];
}
 
-(void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
    CGPoint pt = [[touches anyObject] locationInView:self];
    float dx = pt.x - self.startLocation.x;
    float dy = pt.y - self.startLocation.y;
    CGPoint newcenter = CGPointMake(self.center.x + dx, self.center.y + dy);
    //
    float halfx = CGRectGetMidX(self.bounds);
    newcenter.x = MAX(halfx, newcenter.x);
    newcenter.x = MIN(self.superview.bounds.size.width - halfx, newcenter.x);
    //
    float halfy = CGRectGetMidY(self.bounds);
    newcenter.y = MAX(halfy, newcenter.y);
    newcenter.y = MIN(self.superview.bounds.size.height - halfy, newcenter.y);
    //
    self.center = newcenter;
    
}
 
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    CGPoint point = self.center;
    if (point.x>[self superview].frame.size.width/2.0) {
        [UIView animateWithDuration:0.2 animations:^{
            
            self.frame = CGRectMake([self superview].frame.size.width-self.frame.size.width, self.frame.origin.y, lc_play_float_view_W, lc_play_float_view_H);
        }];
    }else{
        [UIView animateWithDuration:0.2 animations:^{
            self.frame = CGRectMake(0, self.frame.origin.y, lc_play_float_view_W, lc_play_float_view_H);
        }];
    }
}
 
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
}

-(void)tapMe {
    NSLog(@"touch float icon ....");
     
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
